package com.yin.jia.liang.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.yin.jia.liang.common.Config;
import com.yin.jia.liang.dao.GoodsMapper;
import com.yin.jia.liang.dao.UserMapper;
import com.yin.jia.liang.entity.Goods;
import com.yin.jia.liang.entity.GoodsExample;
import com.yin.jia.liang.entity.User;
import com.yin.jia.liang.entity.UserExample;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class GoodsService {

    @Autowired
    GoodsMapper goodsMapper;

    public Goods findById(Integer id) {
        return goodsMapper.selectByPrimaryKey(id);
    }

    public List<Goods> findByType(int type) {
        GoodsExample ue = new GoodsExample();
        GoodsExample.Criteria criteria = ue.createCriteria();
        criteria.andTypeEqualTo(type);
        ue.setOrderByClause("id desc");
        List<Goods> g = goodsMapper.selectByExample(ue);

        return g;
    }

    public PageInfo<Goods> pageFindByExample(int currentPage, Goods goods) {

        GoodsExample ue = new GoodsExample();
        GoodsExample.Criteria criteria = ue.createCriteria();

        if (goods.getType() != null) {
            criteria.andTypeEqualTo(goods.getType());
        }
        if (goods.getProvince() != null && !"".equals(goods.getProvince())) {
            criteria.andProvinceLike("%" + goods.getProvince() + "%");
        }
        if (goods.getName() != null && !"".equals(goods.getName())) {
            criteria.andNameLike("%" + goods.getName() + "%");
        }
        if (goods.getNation() != null && !"".equals(goods.getNation())) {
            criteria.andNationLike("%" + goods.getNation() + "%");
        }
        ue.setOrderByClause("id desc");

        PageHelper.startPage(currentPage, Config.PAGE_SIZE);
        List<Goods> g = goodsMapper.selectByExample(ue);
        PageInfo<Goods> pageInfo = new PageInfo<>(g);

        return pageInfo;
    }

    public Integer save(Goods goods) {
        int i = goodsMapper.insert(goods);
        return i;
    }

    public int update(Goods goods) {
        int i = goodsMapper.updateByPrimaryKeySelective(goods);
        return i;
    }

    public Integer delete(Integer id) {
        return goodsMapper.deleteByPrimaryKey(id);
    }
}
